/**
 * Candidate number: 35834
 */
package tv.starfu.sequencer.model;

import com.artemis.Entity;
import com.artemis.World;
import com.artemis.utils.Bag;
import java.util.ArrayList;
import net.beadsproject.beads.core.AudioContext;

public final class Sequencer {

    private final World world;
    private final AudioContext ac;
    private final static int BPM = 140;
    private final static int ms = bpm_to_ms(BPM);
    
    private ArrayList<Pattern> drumPatterns;
    private ArrayList<Instrument> instruments;
    private Bag<Entity> sequencedEvents;
   // private Pattern pattern;
    
    public DrumHit drumkit;
    
    public Sequencer(AudioContext ac, World world) {
        this.ac = ac;
        this.world = world;
        
        discoPattern(3);
        discoPattern(4);
        discoPattern(5);
        discoPattern(6);
        discoPattern(7);
        discoPattern(8);
        discoPattern(9);
        discoPattern(10);
        discoPattern(11);
        discoPattern(12);
        discoPattern(13);
    }
    
    public void discoPattern(int start) {
        snarePattern(start);
        kickPattern(start);
        ohPattern(start);
        chPattern(start);
    }
    
    public void snarePattern(int starBar) {
                /* SNARE */
        DrumHit snare = new DrumHit(ac, world, "snare", starBar);
        Pattern pattern = new Pattern(16, 8);
        
        /* Set noteOn to default */
        // Sets all channel 0 to 0.
        pattern.set0to1();
        
        /* Notes ON | CellPhase : Step - Channel ~ Phase ] */
        // Set Note ON : Step / Channel / Phase
        pattern.setCellPhase(4, 0, 1);
        pattern.setCellPhase(12, 0, 1);
        
        // Read channels from step - A 1D array of one step's values
        for(int i = 0; i < pattern.getLength(); ++i) {
            int[] channels_8 = pattern.getStep(i);
            
            // new Hit entity for this step .
            //  : Channels / Start / Sample
            snare.createHit(channels_8, i, 1);
        }
    }
    
    public void kickPattern(int starBar) { 
        /* KICK */
        drumkit = new DrumHit(ac, world, "kick", starBar);
        Pattern pattern = new Pattern(16, 8);
        
        /* Set noteOn to default */
        // Sets all channel 0 to 0.
        pattern.set0to1();
        
        /* Notes ON | CellPhase : Step - Channel ~ Phase ] */
        // Set Note ON : Step / Channel / Phase
        pattern.setCellPhase(0, 0, 1);
        pattern.setCellPhase(4, 0, 1);
        pattern.setCellPhase(8, 0, 1);
        pattern.setCellPhase(12, 0, 1);
        
        // Read channels from step - A 1D array of one step's values
        for(int i = 0; i < pattern.getLength(); ++i) {
            int[] channels_8 = pattern.getStep(i);
            
            // new Hit entity for this step .
            //  : Channels / Start / Sample
            drumkit.createHit(channels_8, i, 1);
        }
    }
    
    public void ohPattern(int starBar) { 
        /* OH */
        DrumHit oh = new DrumHit(ac, world, "oh", starBar);
        Pattern pattern = new Pattern(16, 8);
        pattern.set0to1();
        
        pattern.setCellPhase(4, 0, 1);
        pattern.setCellPhase(12, 0, 1);
        
        for(int i = 0; i < pattern.getLength(); ++i) {
            int[] channels_8 = pattern.getStep(i);
            oh.createHit(channels_8, i, 1);
        }
    }
    public void chPattern(int starBar) { 
        /* CH */
        DrumHit ch = new DrumHit(ac, world, "ch", starBar);
        Pattern pattern = new Pattern(16, 8);
        pattern.set0to1();
        pattern.setCellPhase(0, 0, 1);
        pattern.setCellPhase(2, 0, 1);
        pattern.setCellPhase(4, 0, 1);
        pattern.setCellPhase(6, 0, 1);
        pattern.setCellPhase(8, 0, 1);
        pattern.setCellPhase(10, 0, 1);
        pattern.setCellPhase(12, 0, 1);
        pattern.setCellPhase(14, 0, 1);
        for(int i = 0; i < pattern.getLength(); ++i) {
            int[] channels_8 = pattern.getStep(i);
            ch.createHit(channels_8, i, 2);
        }
    }

    public static int bpm_to_ms(int bpm) { // Convert BPM to ms
        float mins_per_beat = 1f / bpm; // How many minutes per beat?
        float sec_per_beat = mins_per_beat * 60; // Seconds per beat
        float ms_per_beat = sec_per_beat * 1000; // Milliseconds per beat

        return (int) ms_per_beat;
    }
}

        
//        Looks nice huh?  
//        pattern.setCellPhase(0, 0, 1);     // Drum on
//        pattern.setCellPhase(0, 1, 1);     // Pitch
//        pattern.setCellPhase(0, 2, 1);     // Velocity
//        pattern.setCellPhase(0, 3, 1);     // Sustain    